package com.siruier.boss.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.siruier.boss.R;

import java.lang.reflect.Method;
import java.util.LinkedList;

import androidx.annotation.ColorRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

public class ViewUtils {
    public static void changeToolbarBackColor(AppCompatActivity activity, @ColorRes int color) {
        Drawable upArrow = ContextCompat.getDrawable(activity, R.drawable.abc_ic_ab_back_material);
        upArrow.setColorFilter(ContextCompat.getColor(activity, color), PorterDuff.Mode.SRC_ATOP);
        activity.getSupportActionBar().setHomeAsUpIndicator(upArrow);
    }

    public static <T extends View> T findViewByClass(View rootView, Class<T> clz) {
        LinkedList<View> views = new LinkedList<>();
        views.push(rootView);
        while (!views.isEmpty()) {
            View popView = views.pop();
            if (popView instanceof ViewGroup) {
                ViewGroup view = (ViewGroup) popView;
                for (int i = 0; i < view.getChildCount(); i++) {
                    View child = view.getChildAt(i);
                    if (child.getClass().isAssignableFrom(clz))
                        return (T) child;
                    if (child instanceof ViewGroup)
                        views.push(child);
                }
            }
        }
        return null;
    }

    public static void setVisibility(int visibility, View... view) {
        for (View item : view) {
            item.setVisibility(visibility);
        }
    }

    public static boolean checkVisibility(int visibility, View... view) {
        for (View item : view) {
            if (item.getVisibility() == visibility)
                return true;
        }
        return false;
    }

    public static boolean checkEmpty(TextView... view) {
        for (TextView item : view) {
            if (TextUtils.isEmpty(item.getText()))
                return true;
        }
        return false;
    }

    public static int getStatusBarHeight(Resources resources) {
        return resources.getDimensionPixelSize(resources.getIdentifier("status_bar_height", "dimen", "android"));
    }

    /**
     * 获取是否存在NavigationBar
     *
     * @param context
     * @return
     */
    public static boolean checkDeviceHasNavigationBar(Context context) {
        boolean hasNavigationBar = false;
        Resources rs = context.getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                //不存在虚拟按键
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                //存在虚拟按键
                hasNavigationBar = true;
            }
        } catch (Exception e) {
        }
        return hasNavigationBar;
    }

    /**
     * 获取虚拟功能键高度
     *
     * @param context
     * @return
     */
    public static int getVirtualBarHeigh(Context context) {
        int result = 0;
        if (checkDeviceHasNavigationBar(context)) {
            Resources res = context.getResources();
            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = res.getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }
}
